package cn.ibizlab.odoo.core.odoo_gamification.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Igamification_goal;
import cn.ibizlab.odoo.core.odoo_gamification.domain.Gamification_goal;
import cn.ibizlab.odoo.core.odoo_gamification.filter.Gamification_goalSearchContext;
import cn.ibizlab.odoo.core.odoo_gamification.service.IGamification_goalService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[游戏化目标] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Gamification_goalServiceImpl implements IGamification_goalService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Igamification_goalClientService gamification_goalClientService;

    @Override
    public boolean create(Gamification_goal gamification_goal) {
        boolean bOk = false ;
        Igamification_goal clientModel = this.convert2Model(gamification_goal,null);
        gamification_goalClientService.create(clientModel) ;
        this.convert2Domain(clientModel,gamification_goal);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Igamification_goal clientModel = gamification_goalClientService.createModel();
        clientModel.setId(id);
        gamification_goalClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Gamification_goal> gamification_goals, int batchSize) {

                        //gamification_goalClientService.createBatch(gamification_goals,batchSize) ;

    }

    @Override
    public boolean update(Gamification_goal gamification_goal) {
        boolean bOk = false ;
        Igamification_goal clientModel = this.convert2Model(gamification_goal,null);
        gamification_goalClientService.update(clientModel) ;
        this.convert2Domain(clientModel,gamification_goal) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Gamification_goal> gamification_goals, int batchSize) {

        //gamification_goalClientService.updateBatch(gamification_goals,batchSize) ;

    }

    @Override
    public Gamification_goal get(Integer id) {
        Igamification_goal clientModel = gamification_goalClientService.createModel();
        clientModel.setId(id);
        gamification_goalClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Gamification_goal get(Gamification_goal gamification_goal) {
        Gamification_goal retgamification_goal = this.get(gamification_goal.getId()) ;
    	BeanCopier copier=BeanCopier.create(retgamification_goal.getClass(), gamification_goal.getClass(), false);
        copier.copy(retgamification_goal, gamification_goal, null);
        return gamification_goal ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //gamification_goalClientService.removeBatch(gamification_goals) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Gamification_goal> searchDefault(Gamification_goalSearchContext context) {
        List<Gamification_goal> list = new ArrayList<Gamification_goal>();
        Page<cn.ibizlab.odoo.core.client.model.Igamification_goal> clientModelList = gamification_goalClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Igamification_goal clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Gamification_goal>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据游戏化挑战的一般目标查询
     */
    @Override
    public Page<Gamification_goal> selectByOdoo_line(Integer id){
        Gamification_goalSearchContext context = new Gamification_goalSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Gamification_goal> list = new ArrayList<Gamification_goal>();
        Page<cn.ibizlab.odoo.core.client.model.Igamification_goal> clientModelList = gamification_goalClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Igamification_goal clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Gamification_goal>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据游戏化挑战查询
     */
    @Override
    public Page<Gamification_goal> selectByOdoo_challenge(Integer id){
        Gamification_goalSearchContext context = new Gamification_goalSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Gamification_goal> list = new ArrayList<Gamification_goal>();
        Page<cn.ibizlab.odoo.core.client.model.Igamification_goal> clientModelList = gamification_goalClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Igamification_goal clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Gamification_goal>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据游戏化目标定义查询
     */
    @Override
    public Page<Gamification_goal> selectByOdoo_definition(Integer id){
        Gamification_goalSearchContext context = new Gamification_goalSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Gamification_goal> list = new ArrayList<Gamification_goal>();
        Page<cn.ibizlab.odoo.core.client.model.Igamification_goal> clientModelList = gamification_goalClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Igamification_goal clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Gamification_goal>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Gamification_goal> selectByOdoo_create(Integer id){
        Gamification_goalSearchContext context = new Gamification_goalSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Gamification_goal> list = new ArrayList<Gamification_goal>();
        Page<cn.ibizlab.odoo.core.client.model.Igamification_goal> clientModelList = gamification_goalClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Igamification_goal clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Gamification_goal>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Gamification_goal> selectByOdoo_user(Integer id){
        Gamification_goalSearchContext context = new Gamification_goalSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Gamification_goal> list = new ArrayList<Gamification_goal>();
        Page<cn.ibizlab.odoo.core.client.model.Igamification_goal> clientModelList = gamification_goalClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Igamification_goal clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Gamification_goal>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Gamification_goal> selectByOdoo_write(Integer id){
        Gamification_goalSearchContext context = new Gamification_goalSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Gamification_goal> list = new ArrayList<Gamification_goal>();
        Page<cn.ibizlab.odoo.core.client.model.Igamification_goal> clientModelList = gamification_goalClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Igamification_goal clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Gamification_goal>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Igamification_goal convert2Model(Gamification_goal domain , Igamification_goal model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = gamification_goalClientService.createModel() ;
        }

         if(domain.getStart_dateDirtyFlag())
            model.setStart_date(domain.getStart_date());
         if(domain.getTo_updateDirtyFlag())
            model.setTo_update(domain.getTo_update());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getRemind_update_delayDirtyFlag())
            model.setRemind_update_delay(domain.getRemind_update_delay());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getCompletenessDirtyFlag())
            model.setCompleteness(domain.getCompleteness());
         if(domain.getTarget_goalDirtyFlag())
            model.setTarget_goal(domain.getTarget_goal());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getClosedDirtyFlag())
            model.setClosed(domain.getClosed());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getLast_updateDirtyFlag())
            model.setLast_update(domain.getLast_update());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getEnd_dateDirtyFlag())
            model.setEnd_date(domain.getEnd_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCurrentDirtyFlag())
            model.setCurrent(domain.getCurrent());
         if(domain.getDefinition_descriptionDirtyFlag())
            model.setDefinition_description(domain.getDefinition_description());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getDefinition_suffixDirtyFlag())
            model.setDefinition_suffix(domain.getDefinition_suffix());
         if(domain.getChallenge_id_textDirtyFlag())
            model.setChallenge_id_text(domain.getChallenge_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getDefinition_id_textDirtyFlag())
            model.setDefinition_id_text(domain.getDefinition_id_text());
         if(domain.getDefinition_displayDirtyFlag())
            model.setDefinition_display(domain.getDefinition_display());
         if(domain.getLine_id_textDirtyFlag())
            model.setLine_id_text(domain.getLine_id_text());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getDefinition_conditionDirtyFlag())
            model.setDefinition_condition(domain.getDefinition_condition());
         if(domain.getComputation_modeDirtyFlag())
            model.setComputation_mode(domain.getComputation_mode());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getLine_idDirtyFlag())
            model.setLine_id(domain.getLine_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getDefinition_idDirtyFlag())
            model.setDefinition_id(domain.getDefinition_id());
         if(domain.getChallenge_idDirtyFlag())
            model.setChallenge_id(domain.getChallenge_id());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
        return model ;
    }

    public Gamification_goal convert2Domain( Igamification_goal model ,Gamification_goal domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Gamification_goal();
        }

        if(model.getStart_dateDirtyFlag())
            domain.setStart_date(model.getStart_date());
        if(model.getTo_updateDirtyFlag())
            domain.setTo_update(model.getTo_update());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getRemind_update_delayDirtyFlag())
            domain.setRemind_update_delay(model.getRemind_update_delay());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getCompletenessDirtyFlag())
            domain.setCompleteness(model.getCompleteness());
        if(model.getTarget_goalDirtyFlag())
            domain.setTarget_goal(model.getTarget_goal());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getClosedDirtyFlag())
            domain.setClosed(model.getClosed());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getLast_updateDirtyFlag())
            domain.setLast_update(model.getLast_update());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getEnd_dateDirtyFlag())
            domain.setEnd_date(model.getEnd_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCurrentDirtyFlag())
            domain.setCurrent(model.getCurrent());
        if(model.getDefinition_descriptionDirtyFlag())
            domain.setDefinition_description(model.getDefinition_description());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getDefinition_suffixDirtyFlag())
            domain.setDefinition_suffix(model.getDefinition_suffix());
        if(model.getChallenge_id_textDirtyFlag())
            domain.setChallenge_id_text(model.getChallenge_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getDefinition_id_textDirtyFlag())
            domain.setDefinition_id_text(model.getDefinition_id_text());
        if(model.getDefinition_displayDirtyFlag())
            domain.setDefinition_display(model.getDefinition_display());
        if(model.getLine_id_textDirtyFlag())
            domain.setLine_id_text(model.getLine_id_text());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getDefinition_conditionDirtyFlag())
            domain.setDefinition_condition(model.getDefinition_condition());
        if(model.getComputation_modeDirtyFlag())
            domain.setComputation_mode(model.getComputation_mode());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getLine_idDirtyFlag())
            domain.setLine_id(model.getLine_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getDefinition_idDirtyFlag())
            domain.setDefinition_id(model.getDefinition_id());
        if(model.getChallenge_idDirtyFlag())
            domain.setChallenge_id(model.getChallenge_id());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        return domain ;
    }


}
